package com.hntxk.cqzd.flup.controller;


import com.alibaba.fastjson.JSON;
import com.hntxk.core.framework.model.ResponseModel;
import com.hntxk.cqzd.common.action.CqzdBaseController;

import com.hntxk.cqzd.flup.entity.FlupInfo;
import com.hntxk.cqzd.flup.model.FlupAndDiacModel;
import com.hntxk.cqzd.flup.model.FlupInfoAndModel;
import com.hntxk.cqzd.flup.model.FlupListModel;
import com.hntxk.cqzd.flup.service.FlupInfoService;
import com.hntxk.cqzd.flup.service.impl.FlupInfoServiceImpl;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.LogManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Arrays;
import java.util.List;

@Controller
@RequestMapping("cqzd/do/flupinfo")
@Slf4j
public class FlupInfoController extends CqzdBaseController
{

    /** 查询页面的path */
    private final static String LIST_JSP_PATH = "cqzd/flup/flupInfo_list";
    private final FlupInfoService flupInfoService;

    @Autowired
    public FlupInfoController(@Qualifier("flupInfoManageService") FlupInfoService flupInfoService) {
        super(flupInfoService, LogManager.getLogger(FlupInfoController.class));
        this.flupInfoService = flupInfoService;
    }
    /**
     *
     * @return 患者基本信息主页
     */
    @ApiIgnore
    @GetMapping(value = "/index")
    public String index() {
        return LIST_JSP_PATH;
    }


    /**
     * 查询随访信息
     * @param model
     * @return
     */
    @ApiOperation("查询随访信息")
    @RequestMapping(value = "/queryList",method = {RequestMethod.GET,RequestMethod.POST}, produces= PRODUCE_UTF8_JSON)
    @ResponseBody
    public String queryFlupInfo(@ModelAttribute FlupAndDiacModel model) {

        try {
            model.setPagination(request);
            return   toJson(model, flupInfoService.query2Json(FlupInfoServiceImpl.FLUPINFO_MAPPER_NAMESPACE + "query", model, true));
        } catch (Exception e) {
            return getError(e);
        }
    }

    /**
     * 查询随访信息
     * @param model
     * @return
     */
    @ApiOperation("查询随访信息")
    @RequestMapping(value = "/queryFlupByDiacrisisId",method = {RequestMethod.GET,RequestMethod.POST}, produces= PRODUCE_UTF8_JSON)
    @ResponseBody
    public String queryFlupByDiacrisisId(@ModelAttribute FlupAndDiacModel model) {

        try {
            model.setPagination(request);
            return   toJson(model, flupInfoService.query2Json(FlupInfoServiceImpl.FLUPINFO_MAPPER_NAMESPACE + "queryFlupByDiacrisisId", model, true));
        } catch (Exception e) {
            return getError(e);
        }
    }

    /**
     * 查询基础信息
     * @param
     * @return
     */
    @ApiOperation("根据右侧输入条件查询基础信息")
    @RequestMapping(value = "/queryListByKey",method = {RequestMethod.GET,RequestMethod.POST}, produces= PRODUCE_UTF8_JSON)
    @ResponseBody
    public ResponseModel queryFlupInfoByKey(@RequestParam(required = false) String key, String isValid) {
        return flupInfoService.getAllInfo(key,isValid);
    }

    /**
     * 获取随访信息
     *
     * @return
     */
    @ApiOperation(value="获取随访内容",notes = "产前诊断系统")
    @ApiImplicitParam(name = "id", value = "随访id",example = "1",required = true, dataTypeClass = Long.class)
    @GetMapping(value = "/get/{id}", produces= PRODUCE_UTF8_JSON)
    @ResponseBody
    public String get(@PathVariable("id") Long id){
        return toJson(flupInfoService.getById(id));
    }

    /**
     * 获取诊断记录的所有随访信息
     *
     * @return
     */
    @ApiOperation(value="获取诊断记录的所有随访信息",notes = "产前诊断系统")
    @ApiImplicitParam(name = "id", value = "诊断记录id",example = "1",required = true, dataTypeClass = Long.class)
    @GetMapping(value = "/getFlupInfoById/{id}", produces= PRODUCE_UTF8_JSON)
    @ResponseBody
    public String getByDiacrisis(@PathVariable("id") Long id){
        FlupInfo model = new FlupInfo();
        model.setDiacrisisId(id);
        List<FlupInfoAndModel> lists = flupInfoService.getByFkId(id);
        return toJson(lists);
    }

//    /**
//     * 新增随访信息
//     * @param files 随访文件对象
//     * @param json  随访信息对象
//     * @return
//     * @throws Exception
//     */
//    @PostMapping(value = "/updateFlupInfo",produces = PRODUCE_UTF8_JSON)
//    @ResponseBody
//    public String updateDibInfoAndFile(@RequestParam("file") MultipartFile[] files,@RequestParam("json") String json) throws Exception{
//        FlupInfoAndModel flupInfoAndModel= JSON.parseObject(json,FlupInfoAndModel.class);
//        flupInfoService.update(flupInfoAndModel,files);
//        return responseOk();
//    }
//
//    /**
//     * 新增随访信息
//     * @param files 随访文件对象
//     * @param json  随访信息对象
//     * @return
//     * @throws Exception
//     */
//    @PostMapping(value = "/addFlupInfo",produces = PRODUCE_UTF8_JSON)
//    @ResponseBody
//    public String addDibInfoAndFile(@RequestParam("file") MultipartFile[] files,@RequestParam("json") String json) throws Exception{
//        FlupInfoAndModel flupInfoAndModel= JSON.parseObject(json,FlupInfoAndModel.class);
//        flupInfoService.insert(flupInfoAndModel,files);
//        return responseOk();
//    }

    /**
     * 新增随访信息
     * @param model  随访信息对象
     * @return
     * @throws Exception
     */
    @PostMapping(value = "/updateFlupInfo",produces = PRODUCE_UTF8_JSON)
    @ResponseBody
    public String updateDibInfoAndFile(@ModelAttribute FlupListModel model) throws Exception{
        FlupAndDiacModel[] flupAndDiacModels = model.getFlupAndDiacModels();
        flupInfoService.update(Arrays.asList(flupAndDiacModels));
        return responseOk();
    }

    /**
     * 新增随访信息
     * @param model  随访信息对象
     * @return
     * @throws Exception
     */
    @ApiOperation(value="新增随访信息",notes = "产前诊断系统")
    @ApiImplicitParam(name = "json", value = "json",example = "1",required = true, dataTypeClass = String.class)
    @PostMapping(value = "/addFlupInfo",produces = PRODUCE_UTF8_JSON)
    @ResponseBody
    public String addDibInfoAndFile(@ModelAttribute FlupListModel model) throws Exception{
        FlupAndDiacModel[] flupAndDiacModels = model.getFlupAndDiacModels();
        flupInfoService.insert(Arrays.asList(flupAndDiacModels));
        return responseOk();
    }

}
